﻿Imports MySql.Data.MySqlClient
Imports conn
Imports System.Security.Cryptography
Public Class functions
    Public Shared Function DateTimeToEpoch(ByVal DateTimeValue As Date) As Integer
        Try
            Return CInt(DateTimeValue.Subtract(CDate("1.1.1970 00:00:00")).TotalSeconds)
        Catch ex As System.OverflowException
            Return -1
        End Try
    End Function
    Public Shared Function EpochToDateTime(ByVal EpochValue As Integer) As Date
        '
        If EpochValue >= 0 Then
            Return CDate("1.1.1970 00:00:00").AddSeconds(EpochValue)
        Else
            Return CDate("1.1.1970 00:00:00")
        End If

    End Function
    Public Shared Function getMd5Hash(ByVal input As String) As String
        ' Create a new instance of the MD5 object.
        Dim md5Hasher As MD5 = MD5.Create()

        ' Convert the input string to a byte array and compute the hash.
        Dim data As Byte() = md5Hasher.ComputeHash(System.Text.Encoding.Default.GetBytes(input))

        ' Create a new Stringbuilder to collect the bytes
        ' and create a string.
        Dim sBuilder As New System.Text.StringBuilder()

        ' Loop through each byte of the hashed data 
        ' and format each one as a hexadecimal string.
        Dim i As Integer
        For i = 0 To data.Length - 1
            sBuilder.Append(data(i).ToString("x2"))
        Next i

        ' Return the hexadecimal string.
        Return sBuilder.ToString()

    End Function
    Public Shared Function getchatmessages()
        conn.connection.conn()
        Dim command = New MySqlCommand
        command.CommandText = "SELECT * FROM `chat` ORDER BY `timestamp`" 'Query to exectue.
        command.Connection = connection.MyADOConnection
        Dim read = command.ExecuteReader
        Dim text = ""
        While read.Read()
            If read.GetString(2) > functions.DateTimeToEpoch(Now) - 86400 Then
                text = read.GetString(1) & ":" & read.GetString(3) & vbCrLf & text
            Else
                text = read.GetString(1) & ":" & read.GetString(3) & vbCrLf & functions.EpochToDateTime(read.GetString(2)) & vbCrLf & text
            End If

        End While
        connection.MyADOConnection.Close()
        Return text
    End Function
End Class
